/*
 * 一键登录需求新增逻辑，该文件用于处理从app获取手机号和token
 */
class OneClick {
    phone;

    // 考虑到有时候调用app获取手机号时间较长，故此处做超时处理
    request(param, success, fail, timeout = 3000, useLoading = true) {
        if (useLoading) {
            WPT.Modal.loading('加载中...', {
                time: 1000000,
                delay: 500,
            });
        }
        let timer = setTimeout(() => {
            timer = null;
            fail && fail();
            if (useLoading) {
                WPT.Modal.loading.close();
            }
        }, timeout);
        WPT.wptBridge.requestRealPhone(
            param,
            (res) => {
                if (timer === null) {
                    return;
                }
                clearTimeout(timer);
                if (useLoading) {
                    WPT.Modal.loading.close();
                }
                success && success(res);
            },
            (res) => {
                if (timer === null) {
                    return;
                }
                clearTimeout(timer);
                if (useLoading) {
                    WPT.Modal.loading.close();
                }
                fail && fail(res);
            }
        );
    }

    requestPhoneFromApp({ success, fail }) {
        this.request(
            { type: 'phone' },
            ({ code, data }) => {
                if (code === 0) {
                    this.phone = data?.phone || data?.phoneNumber;
                    if (this.phone) {
                        success(this.phone, data);
                    } else {
                        fail();
                    }
                } else {
                    fail();
                }
            },
            fail
        );
    }

    requestTokenFromApp({ type, success, fail }) {
        this.request(
            { type },
            ({ code, msg, data }) => {
                if (code === 0) {
                    success && success(data?.token);
                } else {
                    fail && fail(msg);
                }
            },
            fail
        );
    }
    getPhone() {
        return new Promise((resolve, reject) => {
            this.request(
                { type: 'phone' },
                ({ code, data }) => {
                    if (code === 0) {
                        this.phone = data?.phone || data?.phoneNumber;
                        if (this.phone) {
                            resolve([this.phone, data]);
                        } else {
                            reject();
                        }
                    } else {
                        reject();
                    }
                },
                reject
            );
        });
    }
    //获取一键登录token
    getToken() {
        return new Promise((resole, reject) => {
            this.requestTokenFromApp({
                type: 'token',
                success(token) {
                    //此处增加判断逻辑，当从app中返回的token为空或为空string时直接reject
                    if (token) {
                        resole(token);
                    } else {
                        reject('no token');
                    }
                },
                fail(msg) {
                    reject(msg);
                },
            });
        });
    }
    //获取本机号码token
    getLocalPhoneToken() {
        return new Promise((resole, reject) => {
            this.requestTokenFromApp({
                type: 'localCode',
                success(token) {
                    //此处增加判断逻辑，当从app中返回的token为空或为空string时直接reject
                    if (token) {
                        resole(token);
                    } else {
                        reject('no token');
                    }
                },
                fail(msg) {
                    reject(msg);
                },
            });
        });
    }

    // 判断是否可以使用一键登录
    canUseOneClickInfo(phone) {
        return phone === this.phone;
    }
    // 是否本机校验，判断手机号前三位和后四位是否相等，如果相等，则为本机校验
    isLocalVerify(phone) {
        if (!this.phone) return false;
        return this.phone.substr(0, 3) === phone.substr(0, 3) && this.phone.substr(7, 4) === phone.substr(7, 4);
    }
}

export default new OneClick();
